// ActionScript file
			import com.erclab.xpresso.FormManager;
			import com.erclab.xpresso.ReportManager;
			import com.erclab.xpresso.forms.FormManagerUtil;
			import com.erclab.xpresso.reports.*;
			import mx.core.UIComponent;
			import mx.events.DataGridEvent;
			import mx.controls.TextInput;
			
			public var theForm:FormManager;
			public var theReport:ReportManager;
			public var theFilters:FilterManager;
				
            public function startManager():void{
				UpdateButton.visible = false;
				DeleteButton.visible = false;
				InsertButton.visible = false;
				InsertNewButton.visible = false;
            	if (this._params == null){
            		this._params = new Object();
            	}
            	if (this._gridOnly){
					ReportsGrid.editable = true;
					DetailsSection.width = 0;
					DetailsSection.height = 0;
					DetailsSection.visible = false;
					InsertNewButton.visible = true;
					UpdateButton.visible = false;
					InsertButton.visible = false;
					InsertButton.label = "Insert";
				}				
            }
            
            public function alterObject(action:String):void{
	            MainFormProcessor.url="../jsp/" + _objectName + ".fsubmit";
	            if (_formListener != null){
		            if (action == "insert"){
		            	_formListener.beforeInsert(theForm);
		            } else if (action == "update") {
		            	_formListener.beforeUpdate(theForm);
		            }
	            }
	            theForm.sendForm(MainFormProcessor, action);
	            if (_formListener != null){
		            if (action == "insert"){
		            	_formListener.afterInsert(theForm);
		            	prepareForInsert();
		            } else if (action == "update") {
		            	//_formListener.afterUpdate (  ... todo
		            }
	            }
	            this.lastAction = action;
	        }
	        
	        public function setupData():void{
            	theForm = new FormManager(_objectName);
            	theReport = new ReportManager(_objectName);

				if (this._reportListener != null){
					this._reportListener.beforeReportSend(theReport);
				}
				this._objectName = _objectName;
				// Report
				MainService.url = "../jsp/" + _objectName + ".report";
            	MainService.send(_params);

            	// Form
            	var params:Object = new Object();
				params.action = "insert";
				params.uniqueKeyValue = this._params.uniqueKeyValue;
				params.parentKeyValue = this._params.parentKeyValue;
				this.theForm.theForm.parentKeyValue = this._params.parentKeyValue;
            	MainFormService.url = "../jsp/" + _objectName + ".form.f2x";
            	MainFormService.send(params);
				InsertButton.visible = true;            	
	        	UpdateButton.visible = false;
	        	DeleteButton.visible = false;
	        	InsertNewButton.visible = false;
            	
	        }
	        
	        public function displayObject(theGrid:DataGrid):void{
				FormManagerUtil.fillFormFromDG(theGrid, theForm.getFormElements());
	        	if (_reportListener != null){
		        	_reportListener.itemClicked(theReport);
		        }
     	        if (this._gridOnly){
					InsertButton.label = "Duplicate item";
					UpdateButton.visible = false;
		        	DeleteButton.visible = true;
		        	InsertNewButton.visible = true;		        	     	        	
     	        } else {
	     	        UpdateButton.visible = true;
		        	DeleteButton.visible = true;
		        	InsertNewButton.visible = true;
		        	InsertButton.visible = false;
     	        }
	        }
	        
	        public function deleteSelectedObject(theGrid:DataGrid):void{
	        	deleteService.url = "../jsp/" + _objectName + ".rsubmit";
	        	var theItems:Array = theGrid.selectedItems;
				for each (var item:Object in theItems){
		        	if (_params != null) {
			        	item.uniqueKeyValue = _params.uniqueKeyValue;
			        }
		        	theReport.deleteSelectedObject(deleteService, item);
		  		}
				prepareForInsert();	        									
	        }

			public function mainServiceLoaded(theService:HTTPService, 
												attachingElement:DataGrid):void{
			    theReport.setupData(theService, attachingElement);
			    // Filters
            	theFilters = new FilterManager(theReport.theReport);
            	// TODO:
            	// theFilters.createFilters(theService, reportFilters);
            	if (this._reportListener != null){
	            	_reportListener.afterResult(theReport);
	            }
	            if (lastAction == "insert"){
	            	ReportsGrid.validateNow();
	            	ReportsGrid.scrollToIndex(200);
	            }
			}
			
			public function formWasProcessed():void {
				if (this._gridOnly){
					if (this.lastAction == "update"){
						// Do nothing
					} else {
						MainService.send(this._params);
					}
				} else {
					MainService.send(this._params);
				}
			}
			
			public function prepareForInsert():void{
				if (this._gridOnly){
					InsertNewButton.visible = false;
					InsertButton.label = "Insert";
					InsertButton.visible = true;
					UpdateButton.visible = false;
					DeleteButton.visible = false;
				} else {
					InsertNewButton.visible = false;
					InsertButton.visible = true;
					UpdateButton.visible = false;
					DeleteButton.visible = false;
				}
				theForm.clearForm();
			}
			
			public function gridModified(event:DataGridEvent):void{
				if (ReportsGrid.selectedItem != null){
					var theEditor:TextInput = TextInput(event.currentTarget.itemEditorInstance);
					var newValue:String = theEditor.text;
					var oldValue:String = event.currentTarget.editedItemRenderer.data[event.dataField]; 
					if (newValue == oldValue){
						return;
					}
					
					var objectToModify:Object = ReportsGrid.selectedItem;
					objectToModify[event.dataField] = newValue;
					
					FormManagerUtil.fillFormFromObject(objectToModify, theForm.getFormElements());
					alterObject('update');
					
				}
			}

			public function insertNewClick():void{
				if (this._gridOnly){
					prepareForInsert();
					this.alterObject("insert");
				} else {
					prepareForInsert();
				}
			}